package com.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.catalina.connector.Request;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.core.baseDao.extend.Pager;
import com.manager.KeywordManager;
import com.manager.VoiceresponseManager;
import com.model.Keyword;
import com.model.ModuleConstant;
import com.opensymphony.xwork2.ActionSupport;
import com.util.ServletUtils;
import com.util.TimeUtil;

@ParentPackage("json-default")
@Results({ @Result(name = "jsonResult", type = "json") })
@Namespace("/user")
public class VoiceresponseAction extends ActionSupport{
	
	@Autowired
	private VoiceresponseManager voiceresponseManager;
	@Autowired
	private KeywordManager keywordManager;
	
	private Keyword keyword;
	
	public Keyword getKeyword() {
		return keyword;
	}

	public void setKeyword(Keyword keyword) {
		this.keyword = keyword;
	}

	public String add(){
		
		return "add";
	}
	
	public String addTo(){
		String ret = "";
		String token = ServletUtils.getToken();
		if(keyword.getId()==null){
			ret = "添加成功";
			keyword.getVoiceResponse().setCreateTime(TimeUtil.getDate());
		}else{
			ret = "更新成功";
		}
		keyword.setModel(ModuleConstant.VOICE);
		keyword.getVoiceResponse().setUpdateTime(TimeUtil.getDate());
		keyword.getVoiceResponse().setToken(token);
		keyword.setToken(token);
		keyword.getVoiceResponse().setKeyword(keyword);
		try {
			keywordManager.saveOrUpdate(keyword);
		} catch (Exception e) {
			ret = "数据库出错，请于开发人员联系！";
			return "add";
		}finally{
			ServletActionContext.getRequest().setAttribute("ret", ret);
		}
		return index();
	}
	
	public String index(){
		ServletRequest request = ServletActionContext.getRequest();
		Pager page = new Pager();
		String p = request.getParameter("p");
		if (!StringUtils.isEmpty(p)) {
			page.setCurrPage(Integer.parseInt(p));
		}
		// Map session = ActionContext.getContext().getSession();
		// String token = (String)session.get("token");
		Map<String, Object> params = new HashMap<String, Object>();
		// params.put("token", token);
		params.put("model", ModuleConstant.VOICE);
		page = keywordManager.findByPage("from Keyword ", params, page);
		request.setAttribute("page", page);
		return "index";
	}
	
	public String delete(){
		String ret = "";
		ServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		if(!StringUtils.isEmpty(id)){
			try {
				Keyword key = (Keyword)keywordManager.findUnique("from Keyword k where k.id=? and k.token=?", 
						new Object[]{Integer.parseInt(id),ServletUtils.getToken()});
				keywordManager.delete(key);
			} catch (Exception e) {
				request.setAttribute("ret", "删除失败！数据库出错！");
				return "index";
			}
			ret = "删除成功！";
		}else{
			ret = "请选择要删除的数据！";
		}
		request.setAttribute("ret", ret);
		return index();
	}
	
	public String edit(){
		ServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		keyword =  (Keyword)keywordManager.findUnique("from Keyword k where k.id=? and k.token=?", 
				new Object[]{Integer.parseInt(id),ServletUtils.getToken()});
		return "edit";
	}
}
